package rubik;

public class Solver implements Runnable {
	Cube cube;
        String[] action;
	
	private final int DEFAULT_FREQ = 1000;
	
	
	public Solver(Cube cube, String actions) {
		this.cube = cube;		
                parseActions(actions);
	}
        
        private void parseActions(String actions) {
            String[] split = actions.split("\n");
            action = new String[split.length];
            for (int k = 0; k < split.length; k++) {
                int i = 0;
                int uguali = 0;
                while (uguali < 2) {
                    if (split[k].charAt(i) == '=') {
                        uguali++;
                    }
                    i++;
                }
                if (split[k].charAt(i + 1) != ']') {
                    action[k] = split[k].substring(i, i + 2);
                } else {
                    action[k] = split[k].substring(i, i + 1);
                }
            }
        }
	
    @Override
    public void run() {
        int index = 0;
        cube.getCubeGraphics().forceUpdate(cube, true);
        try {
            Thread.sleep(DEFAULT_FREQ);
        } catch (InterruptedException e) {}
        while (index < action.length) {
            if (action[index].charAt(0) == 'U') {
                if (action[index].length() == 1) {
                    cube.U();
                } else if (action[index].charAt(1) == '2') {
                    cube.U2();
                } else {
                    cube.Ua();
                }
            } else if (action[index].charAt(0) == 'D') {
                if (action[index].length() == 1) {
                    cube.D();
                } else if (action[index].charAt(1) == '2') {
                    cube.D2();
                } else {
                    cube.Da();
                }
            } else if (action[index].charAt(0) == 'F') {
                if (action[index].length() == 1) {
                    cube.F();
                } else if (action[index].charAt(1) == '2') {
                    cube.F2();
                } else {
                    cube.Fa();
                }
            } else if (action[index].charAt(0) == 'B') {
                if (action[index].length() == 1) {
                    cube.B();
                } else if (action[index].charAt(1) == '2') {
                    cube.B2();
                } else {
                    cube.Ba();
                }
            } else if (action[index].charAt(0) == 'L') {
                if (action[index].length() == 1) {
                    cube.L();
                } else if (action[index].charAt(1) == '2') {
                    cube.L2();
                } else {
                    cube.La();
                }
            } else if (action[index].charAt(0) == 'R') {
                if (action[index].length() == 1) {
                    cube.R();
                } else if (action[index].charAt(1) == '2') {
                    cube.R2();
                } else {
                    cube.Ra();
                }
            }
            index++;
            cube.getCubeGraphics().forceUpdate(cube, true);
            try {
                Thread.sleep(DEFAULT_FREQ);
            } catch (InterruptedException e) { }
        }
    }
}
